/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air.pagehandler;

import air.eb.Flightinstance;
import air.sb.AirRemote;
import air.sb.LuftRemote;
import air.sb.SASRemote;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;

/**
 *
 * @author mrxyz
 */
public class ChooseTripBean {

    AirRemote AB = lookupAirBean();
    SASRemote SB = lookupSASBean();
    LuftRemote LB = lookupLuftBean();

    public ArrayList<ArrayList<Flightinstance>> possibleRoutes(String d, String a, ArrayList<Flightinstance> depList, ArrayList<Flightinstance> arrList) {
        ArrayList<ArrayList<Flightinstance>> currentFlights = new ArrayList<ArrayList<Flightinstance>>();

        for (Flightinstance fi : depList)
            System.out.println("Dep: " + fi.getNr());
        System.out.println("----------------");
        for (Flightinstance di : arrList)
            System.out.println("Arr: " + di.getNr());

        for (int i = 0; i < depList.size(); i++) {
            for (int j = 0; j < arrList.size(); j++) {

                if (depList.get(i).getNr().equals(arrList.get(j).getNr()) && depList.get(i).getDepAirport().equals(d) && arrList.get(j).getArrAirport().equals(a)) {
                    System.out.println("Direktflyg nr: " + depList.get(i).getNr());
                    ArrayList<Flightinstance> nonStop = new ArrayList<Flightinstance>();
                    nonStop.add(depList.get(i));
                    currentFlights.add(nonStop);
                }
                if (depList.get(i).getDepAirport().equals(d) && arrList.get(j).getDepAirport().equals(depList.get(i).getArrAirport()) && arrList.get(j).getArrAirport().equals(a)) {
                    System.out.println("1 stop flight: " + depList.get(i).getNr() + " - " + arrList.get(j).getNr());
                    ArrayList<Flightinstance> oneStop = new ArrayList<Flightinstance>();
                    oneStop.add(depList.get(i));
                    oneStop.add(arrList.get(j));
                    currentFlights.add(oneStop);
                }
            }

            for (int k = 0; k < arrList.size(); k++) {
                for (int l = 0; l < arrList.size(); l++) {
                    if (depList.get(i).getDepAirport().equals(d) && depList.get(k).getDepAirport().equals(depList.get(i).getArrAirport()) && depList.get(l).getDepAirport().equals(depList.get(k).getArrAirport()) && arrList.get(l).getArrAirport().equals(a)) {
                        System.out.println("2 stop flight: " + depList.get(i).getNr() + " - " + depList.get(k).getNr() + " - " + arrList.get(l).getNr());
                        ArrayList<Flightinstance> twoStop = new ArrayList<Flightinstance>();
                        twoStop.add(depList.get(i));
                        twoStop.add(depList.get(k));
                        twoStop.add(arrList.get(l));
                        currentFlights.add(twoStop);
                    }
                }
            }
        }
        return currentFlights;
    }

    public ArrayList<ArrayList<Flightinstance>> getFlightList() {

        ArrayList<Flightinstance> arrFlights = new ArrayList<Flightinstance>();
        arrFlights.addAll(AB.getArrivalFlights());
        arrFlights.addAll(LB.getArrivalFlights());
        arrFlights.addAll(SB.getArrivalFlights());

        ArrayList<Flightinstance> depFlights = new ArrayList<Flightinstance>();
        depFlights.addAll(SB.getDepartureFlights());
        depFlights.addAll(LB.getDepartureFlights());
        depFlights.addAll(SB.getDepartureFlights());

        return possibleRoutes(LB.getDep(), LB.getArr(), depFlights, arrFlights);
    }

    private LuftRemote lookupLuftBean() {
        try {
            Context c = new InitialContext();
            return (LuftRemote) c.lookup("java:comp/env/LuftBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private AirRemote lookupAirBean() {
        try {
            Context c = new InitialContext();
            return (AirRemote) c.lookup("java:comp/env/AirBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private SASRemote lookupSASBean() {
        try {
            Context c = new InitialContext();
            return (SASRemote) c.lookup("java:comp/env/SASBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
